package Lesson;

import javax.swing.*;

public class ClassATM {
    public static String userName = "008";
    public static String userPsd = "123";
    public static int money = 3000;

    public static void main(String[] args) {
        init();
        while (true) {
            JOptionPane.showMessageDialog(null, "选择你要进行的操作");
            String choose = JOptionPane.showInputDialog(null, "1-存钱\n2-取钱\n3-查询\n4-修改密码\n5-退出系统");
            switch (choose){
                case "1":
                    moneySave();
                    break;
                case "2":
                    moneyGet();
                    break;
                case "3":
                    informGet();
                    break;
                case "4":
                    psdChange();
                    break;
                case "5":
                    JOptionPane.showMessageDialog(null,"谢谢使用");
                    System.exit(0);
            }
        }
    }

    public static void psdChange(){
        String oldPsd=JOptionPane.showInputDialog(null,"输入你的旧密码");
        if (!oldPsd.equals(userPsd)){
            JOptionPane.showMessageDialog(null,"输入的原密码有误，非法操作");
            return;
        }
        String newPsd=JOptionPane.showInputDialog(null,"输入新密码");
        String rePsd=JOptionPane.showInputDialog(null,"再次输入新密码");
        if(!newPsd.equals(rePsd)){
            JOptionPane.showMessageDialog(null,"输入的新密码不一致，非法操作");
            return;
        }
        userPsd=newPsd;
        JOptionPane.showMessageDialog(null,"密码修改成功");
    }

    public static void informGet(){
        JOptionPane.showMessageDialog(null,"你的余额为"+money);
    }

    public static void moneyGet(){
        boolean isEnough=false;
        while (!isEnough) {
            String sss = JOptionPane.showInputDialog(null, "输入你要取款的金额");
            int get = Integer.parseInt(sss);
            if (get > money) {
                JOptionPane.showMessageDialog(null, "余额不足，无法操作");
                isEnough = false;
            } else {
                JOptionPane.showMessageDialog(null, "取款成功");
                isEnough = true;
                money-=get;
                informGet();
            }
        }
    }

    public static void moneySave(){
        String ss=JOptionPane.showInputDialog(null,"输入存款金额");
        int s=Integer.parseInt(ss);
        money+=s;
        informGet();
    }

    public static void init() {
        for (int i = 0; i < 3; i++) {
            String inputNam = JOptionPane.showInputDialog(null, "请输入账号");
            String inputPwd = JOptionPane.showInputDialog(null, "请输入密码");
            if (inputNam.equals(userName) && inputPwd.equals(userPsd)) {
                JOptionPane.showMessageDialog(null,"登录成功");
                break;
            }else {
                JOptionPane.showMessageDialog(null, "账号或密码错误，你还剩余" + (2 - i) + "次机会");
                if (i == 2) {
                    JOptionPane.showMessageDialog(null, "非法输入，账号冻结");
                    System.exit(0);
                }
            }
        }
    }


}
